<?php
/**
 * @author fld 2021-06-14
 * @desc 登陆业务类
 */
namespace App\Service\Admin;

use Illuminate\Support\Facades\Hash;
use App\Service\Admin\AdminService;

class LoginService
{
    /**
     * @author fld 2021-06-14
     * @desc 哈希密码加密
     * @param unknown $passwd
     * @return string
     */
    public function hashEncry($passwd){
        $passwd_salt = env('PASSWD_SALT');
        return Hash::make($passwd.$passwd_salt);
    }    
    /**
     * @author fld 2021-06-14
     * @desc 登陆操作
     * @param unknown $data
     * @return number[]|string[]|number[]
     */
    public function doLogin($data){
        $adminService = new AdminService();
        $conds[] = ['admin_name','=',$data['username']];
        $users = $adminService->getAdminByCondition($conds);
        if(empty($users)){
            return ['code'=>1,'message'=>'用户名不正确，请重新输入'];
        }else{
            if(verifyHashPassword($data['password'], $users['admin_pwd'])){
                $login_time = time();
                $admin_info = [
                    'id'=>$users['id'],
                    'admin_name'=>$users['admin_name'],
                    'true_name'=>$users['true_name'],
                    'role_id'=>$users['role_id'],
                    'login_time'=>$login_time,
                ];
                session(['admin'=>$admin_info]);
                //更新管理员登陆信息
                $adminService->updateAdminById($users['id'], ['login_time'=>$login_time,'login_ip'=>$data['login_ip']]);
                return ['code'=> 0, 'true_name'=>$users['true_name']];
            }else{
                return ['code'=>2,'message'=>'密码不正确，请重新输入'];
            }
        }
    }
}